package com.yxsa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxsa.entity.Storage;
import com.yxsa.feign.StorageFeignClient;
import com.yxsa.mapper.StockMapper;
import com.yxsa.service.StockService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Storage> implements StockService {

    @Resource
    private StorageFeignClient storageFeignClient;

    @GlobalTransactional
    @Transactional(rollbackFor = Exception.class)
    public String find(String name) {
        LambdaQueryWrapper<Storage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Storage::getName,name);
        Storage storages = this.baseMapper.selectOne(wrapper);
        LambdaUpdateWrapper<Storage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Storage::getId,storages.getId());
        storages.setCount(storages.getCount()-1);
        this.baseMapper.update(storages,updateWrapper);
        storageFeignClient.deduct(storages.getName(),1);
        return "ok";
    }
}
